﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class CongNoNCCAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadNhaCC();
                LoadHienthi();
            }
        }
        /// <summary>
        /// Load nhà CC
        /// </summary>
        public void LoadNhaCC()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            NHACUNGCAPRepository repository=new NHACUNGCAPRepository(new EFRepository<NHACUNGCAP>(),iunitfwork);
            IQueryable<NHACUNGCAP> objNhacc = repository.All();
            Dlnhacc.DataSource = objNhacc;
            Dlnhacc.DataValueField = "MANCC";
            Dlnhacc.DataTextField = "TENNCC";
            Dlnhacc.DataBind();
            Dlnhacc.Items.Insert(0, new ListItem {Text="--Chọn nhà cung cấp---",Value="0" });
        }
        /// <summary>
        /// Load Hiển thị
        /// </summary>
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNONCCRepository repository = new CONGNONCCRepository(new EFRepository<CONGNONCC>(), iunitfwork);
            CONGNONCC objCongno = null;
            string strid = "" + Request["ID"];
            if (!string.IsNullOrEmpty(strid))
            {
                objCongno = repository.GetById(int.Parse(strid));
                Dlnhacc.SelectedValue = objCongno.NHACUNGCAP.MANCC;
                txtsotiendatra.Value = objCongno.TONGTIENDATRA.ToString();
                txtsotienphaitra.Value = objCongno.TONGTIENTRA.ToString();
                txtsotienno.Value = objCongno.TONGNO.ToString();
                txtmacn.Value = objCongno.MACC;
                if (objCongno.TINHTRANG == true)
                {
                    rdtt.Checked = true;
                }
                else
                {
                    rdchuatt.Checked = true;
                }
            }
            string strMANCC = "" + Request["MANCC"];
            if (!string.IsNullOrEmpty(strMANCC))
            {
                Dlnhacc.SelectedValue = strMANCC;
                txtsotienphaitra.Value = Session["Tongtien"].ToString();
            }

        }
        public void capnhap()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNONCCRepository repository = new CONGNONCCRepository(new EFRepository<CONGNONCC>(), iunitfwork);

            CONGNONCC objCongno = null;
            string strid = "" + Request["ID"];
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                iscapnhap = false;
                objCongno = repository.GetById(int.Parse(strid));
            }
            else
            {
                objCongno = new CONGNONCC();
                iscapnhap = true;
            }
            if (Dlnhacc.SelectedValue.Equals("0"))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert ('Vui lòng chọn khách hàng')</script>");
                return;
            }
             objCongno.MANCC = Dlnhacc.SelectedValue;
             objCongno.TONGTIENDATRA = int.Parse(txtsotiendatra.Value);
             objCongno.TONGTIENTRA = int.Parse(txtsotienphaitra.Value);
             objCongno.TONGNO = int.Parse(txtsotienphaitra.Value) - int.Parse(txtsotiendatra.Value);
             objCongno.MACC = txtmacn.Value;
             if (rdtt.Checked)
             {
                 objCongno.TINHTRANG = true;
             }
             if (rdchuatt.Checked)
             {
                 objCongno.TINHTRANG = false;
             }
             List<string> LstString = dataprovider.Entity.CONGNONCCs.Select(t => t.MACC).ToList();
                if (iscapnhap)
                {
                    foreach (string cn in LstString)
                    {
                        if (cn.Trim() ==txtmacn.Value.Trim())
                        { 
                           
                            RegisterClientScriptBlock("Thông báo", "<script> alert('Đã tồn tại')</script>");
                            return;
                        }

                    }
                    repository.Add(objCongno);
                }
                repository.Save();
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }

        protected void ltbnthemmoi_Click(object sender, EventArgs e)
        {
            txtmacn.Value = "";
            txtsotiendatra.Value = "";
            txtsotienno.Value = "";
            txtsotienphaitra.Value = "";
            Dlnhacc.SelectedValue = "0";
            Response.Redirect("CongNoNCCAdd.aspx");
        }
        /// <summary>
        /// Lấy theo Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// pu
        public CONGNONCC LaytheoId(string id)
        {
            CONGNONCC objCongno = dataprovider.Entity.CONGNONCCs.Where(p => p.MANCC == id).FirstOrDefault();
            return objCongno;
        }
        public void Insert(CONGNONCC data)
        {
            dataprovider.Entity.CONGNONCCs.AddObject(data);
        }
       
    }
}